package com.kura.uednews.dantri;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

public class DanTriHandler extends DefaultHandler {
	NewsFeed feed;
	NewsItem item;

	boolean feedTitleHasBeenRead = false;
	boolean feedPubDateHasBeenRead = false;

	boolean isTitle = false;
	boolean isDescription = false;
	boolean isLink = false;
	boolean isPubDate = false;

	public DanTriHandler() {
	}

	public NewsFeed getFeed() {
		return feed;
	}

	public void startDocument() throws SAXException {
		feed = new NewsFeed();
		item = new NewsItem(); // an item to temporarily store feed data
	}

	public void endDocument() throws SAXException {
	}

	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {

		if (qName.equals("item")) {
			// create a new item
			item = new NewsItem();
			return;
		} else if (qName.equals("title")) {
			isTitle = true;
			return;
		} else if (qName.equals("description")) {
			isDescription = true;
			return;
		} else if (qName.equals("link")) {
			isLink = true;
			return;
		} else if (qName.equals("pubDate")) {
			isPubDate = true;
			return;
		}
	}

	public void endElement(String namespaceURI, String localName, String qName)
			throws SAXException {
		if (qName.equals("item")) {
			feed.addItem(item);
			return;
		}
	}

	public void characters(char ch[], int start, int length) {
		String s = new String(ch, start, length);
		// Log.i("RSSHandler", "s: " + s);

		if (isTitle) {
			if (feedTitleHasBeenRead == false) {
				feed.setTitle(s);
				feedTitleHasBeenRead = true;
			} else {
				item.setTitle(s);
			}
			isTitle = false;
		} else if (isLink) {
			Log.e("link", s);
			item.setLink(s);
			isLink = false;
		} else if (isDescription) {
			item.setDescription(s);
			isDescription = false;
		} else if (isPubDate) {
			if (feedPubDateHasBeenRead == false) {
				feed.setPubDate(s);
				feedPubDateHasBeenRead = true;
			} else {
				item.setPubDate(s);
			}
			isPubDate = false;
		}
	}
}
